<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 * Abstract view.
 * 
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel_View
 */
abstract class View extends Dictionary implements IView, IRenderable
{

  /**
   * @var string
   */
  protected $template;

  /**
   * @var mixed
   */
  protected $layout = false;

  /**
   * @var array
   */
  protected $__helpers = array();

  /**
   * 
   * @var IApplicationController
   */
  protected $app;
  
  /**
   * 
   * @var Config
   */
  protected $config;

  /**
   * 
   * @var IRequest
   */
  protected $request;

  /**
   * 
   * @var IResponse
   */
  protected $response;

  /**
   * 
   * @var IResponse
   */
  protected $router;
  
  public function __construct(IApplicationController $app)
  {
    $this->app = $app;
    $this->config = $app->getConfig();
    $this->request = $app->getRequest();
    $this->response = $app->getResponse();
    $this->router = $app->getRouter();
  }
  
  /**
   *
   */
  public function __get($name)
  {
    if (! isset($this->__helpers[$name])) {
      $this->__helpers[$name] = Helper::factory($name, $this->app);
    }

    return $this->__helpers[$name];
  }

  /**
   *
   * @return View
   */
  public static function factory($type, IApplicationController $app)
  {
    $class = $type . 'View';
    $view = new $class($app);
    return $view;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/IRenderable#getLayout()
   */
  public function getLayout()
  {
    if ($this->layout === false) {
      return false;
    }

    elseif (empty($this->layout)) {
      $layout = $this->config->get('home_dir').'/layouts/default.php';
    }

    elseif (path_is_absolute($this->layout)) {
      $layout = $this->layout;
    }

    else {
      $layout = $this->config->get('home_dir').'/layouts/'.$this->layout.'.php';
    }

    if (! is_readable($layout))
    throw new NotFoundException(sprintf('Layout file not found: <b>%s</b>', $layout));

    return $layout;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/IRenderable#getTemplate()
   */
  public function getTemplate()
  {
    if (empty($this->template)) {
      throw new NotFoundException('Template file not set');
    }

    elseif (path_is_absolute($this->template)) {
      $template = $this->template;
    }

    else {
      $template = $this->config->get('home_dir').'/'.$this->template.'_view.php';
    }

    if (! is_readable($template))
    throw new NotFoundException(sprintf('Template file not found: <b>%s</b>', $template));

    return $template;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/IRenderable#setTemplate($template)
   */
  public function setTemplate($template)
  {
    $this->template = $template;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/IRenderable#setLayout($layout)
   */
  public function setLayout($layout)
  {
    $this->layout = $layout;
  }
}

?>